﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DslModeling = global::Microsoft.VisualStudio.Modeling;
using devtm.Editor.TypeSelector;
using EnvDTE;
using VSLangProj;
using System.IO;
using devtm.AutoMapper.CustomCode.Dsl2Dte;
using CSharpParser.SCharpType;
using Microsoft.RestrictedUsage.CSharp.Semantics;

namespace devtm.AutoMapper
{
    public class DynamicTypeProvider : ITypeProvider
    {

        public DynamicTypeProvider()
        {
            
            SourceTypes.Source.Add(this);

        }



        public IEnumerable<Type> GetTypes(DslModeling.Store store, HashSet<String> namespaces)
        {

            var i = GetProjectListe(store).ToList();

            foreach (Project prj in i)
            
                foreach (CSharpType type in Parser.GetTypes(prj))
                {
                    CSType t = new CSType(type);
                    yield return t;
                }
            
        }


        public IEnumerable<Type> GetTypes(DslModeling.Store store, HashSet<String> namespaces, Func<Type, bool> filter)
        {

            var i = GetProjectListe(store).ToList();

            foreach (Project prj in i)

                foreach (CSharpType type in Parser.GetTypes(prj))
                {
                    CSType t = new CSType(type);

                    if (filter(t))
                        yield return t;

                }

        }



        private IEnumerable<Project> GetProjectListe(DslModeling.Store store)
        {
            Project project = Store2DTE.GetProjectForStore(store);

            if (project == null)
                yield break;

            yield return project;

            VSProject vsProject = project.Object as VSProject;
            foreach (Reference item in vsProject.References)
            {

                if (item.SourceProject != null)
                {

                    // si la Source project n'est pas null c'est que le type est contenu dans la solution.
                    //la chargé var locker Language dll.
                    string path = item.Path;

                    var CopyLocal = item.CopyLocal;

                    //if (!File.Exists(path))
                    //{
                    //    ReferenceError err = new ReferenceError() { Name = item.Name, Source = vsProject.Project.Name };
                    //    SourceTypes.Errors.A
                    //    _errors.Add(err);
                    //    continue;
                    //}
                    yield return item.SourceProject;

                }


            }
        }
        

                
        #region Disposable


        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }


        ~DynamicTypeProvider()
        {
            Dispose(false);
        }


        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {

            if (disposing)
            {
                // free managed resources
            
            }

            SourceTypes.Source.Remove(this);

        }

        #endregion

    }
}
